//
//  WmcHomeAspectRatioView.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/7/10.
//

import UIKit
import YPGraphicEngine
import RxRelay
import YPBedrock

class WmcHomeAspectRatioView: WmcBaseView {
    
    typealias AspectRatio = YPGESunglasses.AspectRatio
    
    var aspectRatio: AspectRatio = .ar_4_3 {
        didSet {
            selectBtn?.isSelected = false
            let button = buttons.first { $0.aspectRatio == aspectRatio }
            button?.isSelected = true
            selectBtn = button
        }
    }

    /// stackView
    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .horizontal
        stackV.spacing = 40.0.wmc_autoHor
        stackV.distribution = .equalSpacing
        stackV.alignment = .fill
        return stackV
    }()
    /// 当前选中的按钮
    private var selectBtn: RatioButton?
    /// 所有按钮
    private lazy var buttons: [RatioButton] = []
    /// 选中画幅比例回调
    let didSelectedRatio: PublishRelay<AspectRatio> = .init()
    
    // MARK: UI
    override func makeUI() {
        super.makeUI()
        let hMargin = 24.0.wmc_autoHor
        // self
        backgroundColor = UIColor.wmc_white_FFFFFF
        layer.cornerRadius = 8.0
        clipsToBounds = true
        // stackView
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.left.equalTo(hMargin)
            make.right.equalTo(-hMargin)
            make.top.bottom.equalToSuperview()
        }
        // 添加按钮
        AspectRatio.allCases.forEach { ratio in
            let button = RatioButton(style: .imageTop, space: 6.0)
            button.aspectRatio = ratio
            stackView.addArrangedSubview(button)
            button.snp.makeConstraints { make in
                make.width.equalTo(35.0.wmc_autoHor)
            }
            self.buttons.append(button)
            button.rx.tap.subscribe(onNext: {
                [weak self] btn in
                guard let self = self else { return }
                self.aspectRatio = ratio
                self.didSelectedRatio.accept(ratio)
            }).disposed(by: rx.disposeBag)
        }
        // 默认选中
        self.aspectRatio = .ar_4_3
    }

}

extension WmcHomeAspectRatioView {
    
    class RatioButton: WmcStyleButton {
        
        override var isHighlighted: Bool {
            set {
                
            }
            get {
                return false
            }
        }
        
        var aspectRatio: AspectRatio = .ar_4_3 {
            didSet {
                let image = aspectRatio.info.image
                let title = aspectRatio.info.name
                setImage(image, for: .normal)
                setImage(image.yp.set(tintColor: UIColor.wmc_mainTheme), for: .selected)
                setTitle(title, for: .normal)
            }
        }
        
        override var isSelected: Bool {
            didSet {
                titleLabel?.backgroundColor = isSelected ? UIColor.wmc_mainTheme : UIColor.clear
            }
        }
        
        override init(style: WmcStyleButton.Style, space: Double) {
            super.init(style: style, space: space)
            setTitleColor(UIColor.wmc_grayH2_323233, for: .normal)
            setTitleColor(UIColor.wmc_white_FFFFFF, for: .selected)
            titleLabel?.font = 13.wmc_scaleFont()
            titleLabel?.layer.cornerRadius = 2.0
            titleLabel?.clipsToBounds = true
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        override func layoutSubviews() {
            super.layoutSubviews()
            titleLabel?.frame.size.height = 20.0.wmc_autoHor
        }
    }
}
